package icasue.runninginvoke.task.redis;

import lombok.Data;
import org.apache.logging.log4j.util.Strings;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/11/12 上午11:23
 * @UpdateDate:
 * @Description:
 */
@Data
public abstract class AbstractRedisAsyncTask implements AsyncTaskOperate{

    protected String hashKeyHome = "runninginvoke:task:key";

    public static final String TOUCH_EXECUTE_LUA = String.join(
            Strings.EMPTY,
            "local touch = redis.call('HGET',KEYS[1],KEYS[2]);",
            "  if(touch) then " ,
            "       return 0;",
            "   else",
            "       redis.call('HSET',KEYS[1],KEYS[2],ARGV[1]);",
            "       return 1;",
            "   end;"
    );

    public static final String GET_EXECUTE_LUA = String.join(
            Strings.EMPTY,
            "local touch = redis.call('HGET',KEYS[1],KEYS[2]);",
            "  if(touch) then " ,
            "       return touch;",
            "  else",
            "       return nil;",
            "  end;"
    );

    public static final String DESTROY_EXECUTE_LUA = String.join(
            Strings.EMPTY,
            "redis.call('HDEL',KEYS[1],KEYS[2]); return 1;"
    );

    public static final String UPDATE_TASK_IDENTITY = String.join(
            Strings.EMPTY,
            "redis.call('HSET',KEYS[1],KEYS[2],ARGV[1]); return 1;"
    );

    public static final String CLEAR_TASK_IDENTITY = String.join(
            Strings.EMPTY,
            "redis.call('del',KEYS[1]); return 1;"
    );

    /* Un used. */
    public static final String CLEAR_TASK_TRIGGER = String.join(
            Strings.EMPTY,
            "redis.call('del',KEYS[1] .. '@Trigger' ); return "
    );

    abstract protected void init();

    public void clear(){
        // Any operator which implement this class,
        // should override this method, to confirm previous icasue.runninginvoke.task's identities has been empty.
    };

}
